package com.wlyuan.open.remote.converter;

import com.alibaba.fastjson.JSON;
import com.wlyuan.open.remote.message.OrderMessage;
import com.wlyuan.open.vo.push.order.OrderDetailVO;
import lombok.var;

import java.time.LocalDateTime;

/**
 * @author
 * 运单转订单信息
 */
public class WaybillMessageConverter implements StringMessageConverter<OrderMessage>{

    @Override
    public OrderMessage convert(String source) {
        var message = JSON.parseObject(String.valueOf(JSON.parse(source)));
        var waybill = message.getJSONObject("waybill");
        OrderMessage orderMessage = new OrderMessage();
        OrderDetailVO orderDetail = new OrderDetailVO();
        orderDetail.setTenantId(waybill.getLong("tenantId"));
        var isRootWayBill = waybill.getInteger("isRootWaybill");
        if(!(isRootWayBill!=null && isRootWayBill==1)) {
            LocalDateTime acceptTime = LocalDateTime.now();
            orderDetail.setAcceptTime(acceptTime);
            orderDetail.setOrderId(waybill.getLong("orderId"));
            var feeInfo = waybill.getJSONObject("waybillPrice");
            if (feeInfo != null) {
                orderDetail.setTotalFee(feeInfo.getBigDecimal("totalFee"));
            }
            orderMessage.setOrder(orderDetail);
            return orderMessage;
        }
        return null;
    }
}
